package com.seed.util;

import cn.hutool.core.util.StrUtil;
import com.seed.core.builder.pojo.Join1;
import com.seed.core.builder.pojo.OnCondition1;
import com.seed.core.builder.pojo.Table1;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;

public class Join1Kit {

    public static Join1 buildJoin1(Relation relation, String basePath, String crossOrColumn) {
        Seed targetSeed = relation.targetSeed();
        String finalPath = StrKit.concat(basePath, crossOrColumn);
        String targetTableAlias = StrUtil.toCamelCase(finalPath);
        Table1 targetTable = new Table1(targetSeed.getTableName()).as(targetTableAlias);

        String selfTableAlias1 = basePath;
        String targetTableAlias1 = StrUtil.toCamelCase(crossOrColumn);
        Table1 selfTable1 = new Table1(relation.selfSeed().getTableName()).as(selfTableAlias1);
        Table1 targetTable1 = new Table1(targetSeed.getTableName()).as(targetTableAlias1);
        String inferSelfColumn = relation.inferSelfColumn();
        String inferTargetColumn = relation.inferTargetColumn();

        OnCondition1 on = new OnCondition1(selfTable1, inferSelfColumn, targetTable1, inferTargetColumn);
        on.setExtra(SeedKit.formatExtra(relation.getExtraSeed(), relation.getExtra()));

//        joins.putIfAbsent(finalPath, new Join1(targetTable, on));
        return new Join1(targetTable, on);
    }
}
